package com.umpay.util;


import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.exception.BusinessException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;
import java.util.Map;


public class HsplusUtil {
	public final static String SIGN = "sign";
	public static final Logger log = LogManager.getLogger(HsplusUtil.class);
	
	public static Map<String, Object> sign(Map<String, Object> signMap){
        log.info(String.format("【响应惠商】参数转换完毕，开始加签:[%s]", signMap.toString()));
        
    	Object[] obj = signMap.keySet().toArray();
		Arrays.sort(obj);
		String plain="",str="",value = "";
		StringBuffer signString = new StringBuffer();
		for(int i = 0 ; i < obj.length ; i++){
			str = obj[i].toString();
			value = signMap.get(str).toString();
			if(StringUtil.isEmpty(value)){
				continue;
			}
			if(!str.startsWith(SIGN)){
				signString.append(value).append("|");
			}
		}
		plain = signString.subSequence(0, signString.length()-1).toString();
                
        log.info(String.format("【响应惠商】获取加签字符串为:[%s]", plain));
		try {
			String sign = SignEnc.sign(plain, "UTF-8", getPKPath());
			log.info(String.format("【响应惠商】获取加签结果为:[%s]", sign));
		    signMap.put("sign", sign);
		} catch (SignEncException e) {
			log.error("签名异常", e);
	        throw new BusinessException(MerAccessEC.MERSIGN_ERR, "签名异常");
		}
        return signMap;
	}

	public static void  verifySign(Map<String, Object> reqMap){
		Object[] obj = reqMap.keySet().toArray();
		Arrays.sort(obj);
		String plain="",str="",value = "";
		StringBuffer signString = new StringBuffer();
		for(int i = 0 ; i < obj.length ; i++){
			str = obj[i].toString();
			value = reqMap.get(str).toString();
			if(StringUtil.isEmpty(value)){
				continue;
			}
			if(!str.startsWith(SIGN)){
				signString.append(str).append("=").append(value).append("&");
			}
		}
		plain = signString.subSequence(0, signString.length()-1).toString();
		log.info("验签字符串为:"+plain);
		String sign = (String) reqMap.get("sign");
		try {
			if(!SignEnc.verify(plain,sign, "UTF-8",getHSCertPath())) {
				log.error(String.format("【惠商签名验证】验签失败,plain=[%s];sign=[%s];", plain, sign));
	            throw new BusinessException(MerAccessEC.MERSIGN_ERR, "验证惠商签名失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(String.format("【惠商签名验证】验签失败,plain=[%s];sign=[%s];errorMsg[%s]", plain, sign,e.getMessage()));
            throw new BusinessException(MerAccessEC.MERSIGN_ERR, "验证惠商签名失败");
		}
	}
	public static String getHSCertPath() {
		PropertyUtil  propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
    	String path = propertyUtil.getProperty("huishang.cert.path");
    	if(StringUtils.isEmpty(path)){
    		throw new BusinessException(MerAccessEC.SYSTEM_PARAM_NOT_CONFIGURE,"惠商证书路径未配置");
    	}
		return path;
	}
	public static String getPKPath() {
		PropertyUtil  propertyUtil = (PropertyUtil)SpringUtil.getBean("propertyUtil");
    	String path = propertyUtil.getProperty("huishang.pk.path");
    	if(StringUtils.isEmpty(path)){
    		throw new BusinessException(MerAccessEC.SYSTEM_PARAM_NOT_CONFIGURE,"惠商集成商户私钥未配置");
    	}
		return path;
	}
	
	
}